package com.aryankh.service.impl;

import com.aryankh.entity.KhMenu;
import com.aryankh.dao.KhMenuDao;
import com.aryankh.service.KhMenuService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 菜单权限表(KhMenu)表服务实现类
 *
 * @author makejava
 * @since 2022-05-28 07:19:50
 */
@Service("khMenuService")
public class KhMenuServiceImpl implements KhMenuService {
    @Resource
    private KhMenuDao khMenuDao;

    /**
     * 通过ID查询单条数据
     *
     * @param menuId 主键
     * @return 实例对象
     */
    @Override
    public KhMenu queryById(Long menuId) {
        return this.khMenuDao.queryById(menuId);
    }

    /**
     * 分页查询
     *
     * @param khMenu 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<KhMenu> queryByPage(KhMenu khMenu, PageRequest pageRequest) {
        long total = this.khMenuDao.count(khMenu);
        return new PageImpl<>(this.khMenuDao.queryAllByLimit(khMenu, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param khMenu 实例对象
     * @return 实例对象
     */
    @Override
    public KhMenu insert(KhMenu khMenu) {
        this.khMenuDao.insert(khMenu);
        return khMenu;
    }

    /**
     * 修改数据
     *
     * @param khMenu 实例对象
     * @return 实例对象
     */
    @Override
    public KhMenu update(KhMenu khMenu) {
        this.khMenuDao.update(khMenu);
        return this.queryById(khMenu.getMenuId());
    }

    /**
     * 通过主键删除数据
     *
     * @param menuId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long menuId) {
        return this.khMenuDao.deleteById(menuId) > 0;
    }
}
